package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.SystemNewsContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.SystemNewsPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.SystemNewsAdapter;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/23 15:04
 * 功能描述： 1系统消息列表
 * 联系方式：1037438704@qq.com
 */

public class SystemNewsActivity extends BaseActivity implements SystemNewsContract, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int count;
    private SystemNewsAdapter systemNewsAdapter;
    private SystemNewsPresenter presenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_system_news;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        presenter = new SystemNewsPresenter(this);
        count = getIntent().getExtras().getInt("count");
        if (count == 1) {
            textCenter.setText("系统消息");
        } else if (count == 2) {
            textCenter.setText("广播消息");
        } else if (count == 3) {
            textCenter.setText("评价消息");
        } else if (count == 4) {
            textCenter.setText("缴费提醒");
        }
        imageleftFinish.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        systemNewsAdapter = new SystemNewsAdapter(R.layout.item_order_reminding);
        recyclerView.setAdapter(systemNewsAdapter);
        systemNewsAdapter.setOnItemClickListener(this);
        systemNewsAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                MessageDialog.show(me, "温馨提示", "是否删除此条信息？", "删除", "取消")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                String id = systemNewsAdapter.getData().get(position).get("id");
                                if (TextUtils.isEmpty(id)) {
                                    return true;
                                }
                                HttpRequest.POST(me, "Msg/delMsg"
                                        , new Parameter()
                                                .add("token", token)
                                                .add("id", id)
                                        , new ResponseListener() {
                                            @Override
                                            public void onResponse(String response, Exception error) {
                                                if (error == null) {
                                                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                                    if (map.get("code").equals("1")) {
                                                        systemNewsAdapter.remove(position);
                                                    }
                                                }
                                            }
                                        });
                                return false;
                            }
                        });
                return false;
            }
        });
    }

    @OnClick({R.id.image_left_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            default:
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getMsgList(token, count + "");
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Map<String, String> map = systemNewsAdapter.getData().get(position);
        if (map == null) {
            return;
        }
        DetailsWebViewActivity.start(me, count, 3, map.get("id"));
    }

    public static void start(Context me, int count) {
        Intent intent = new Intent(me, SystemNewsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("count", count);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    @Override
    public void Success(List<Map<String, String>> data) {
        systemNewsAdapter.setNewData(data);
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }
}
